"use strict";

function isObj(obj) {
    return Object.prototype.toString.call(obj) === "[object Object]";
}

function spinnerStart(msg, spinnerStr = "|/-\\") {
    const Spinner = require("cli-spinner").Spinner;
    var spinner = new Spinner(`${msg} %s`);
    spinner.setSpinnerString(spinnerStr);
    spinner.start();
    return spinner;
}

function sleep(time = 1000) {
    return new Promise((res) => setTimeout(res, time));
}
// 兼容windows和macOs系统命令
function exec(command, args, options) {
    const win32 = process.platform === "win32";
    const cmd = win32 ? "cmd" : command;
    const cmdArgs = win32 ? ["/c"].concat(command, args) : args;
    return require("child_process").spawn(cmd, cmdArgs, options || {});
}

// 异步兼容windows和macOs系统命令
function execSync(command, args, options) {
    return new Promise((res, rej) => {
        const p = exec(command, args, options);
        p.on("error", (e) => {
            rej(error);
        });
        p.on("exit", (c) => {
            res(c);
        });
    });
}

module.exports = {
    isObj,
    spinnerStart,
    sleep,
    exec,
    execSync,
};
